//
//  GetData.m
//  Moment
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 谭泽源. All rights reserved.
//

#import "GetData.h"
#import <AFNetworking.h>

@implementation GetData

+ (void)getDataWithURL:(NSString *)url block:(GetDataBlock)block
{
    //最好编码一下
//    NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    

    
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        block(responseObject);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误信息%@", error);

    }];
    

}


+ (void)getDataWithURL:(NSString *)url postDic:(NSDictionary *)dic block:(GetDataBlock)block
{
    
    AFHTTPSessionManager *netManager = [AFHTTPSessionManager manager];
    netManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [netManager POST:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        block(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"错误信息%@", error);
    }];
//    [netManager POST:url parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
//        block(responseObject);
//        
//    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        
//        NSLog(@"错误信息%@", error);
//
//    }];
    
    
}

+ (void)getUrl:(NSString *)url
          body:(id)body
        result:(HXResult)result
    headerFile:(NSDictionary *)headerFile
       success:(void (^)(id result))success
       failure:(void (^)(NSError *error))failure
{
    // 1.获取网络请求管理类
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    // 2.为网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile.allKeys) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    // 3.设置返回值类型
    switch (result) {
        case HXData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case HXJSON:
            break;
        case HXXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    // 4.设置网络请求返回值所支持的参数类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain",@"application/x-javascript", nil]];
    // 让网络指示器旋转
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    //    manager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
    
    // 5.发送网络请求
    
    [manager GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
//            NSLog(@"%@", [NSOperation.response allHeaderFields]);
            // 成功回调
            success(responseObject);
            
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            
            // 存储的沙盒路径
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            
            //             NSLog(@"--------%@",path_doc);
            
            // 归档
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            NSLog(@"%@", error);
            // 失败回调
            failure(error);
            
            // 缓存的文件夹
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
            success(result);
            
        }

        
    }];
 
}
+ (void)postUrl:(NSString *)url
           body:(id)body
         result:(HXResult)result
   requestStyle:(HXRequestStyle)requestStyle
     headerFile:(NSDictionary *)headerFile
        success:(void (^)(id result))success
failure:(void (^)(NSError *error))failure
{
    // 1.获取网络请求管理类
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2.网络请求请求体Body的类型
    switch (requestStyle) {
        case HXRequestJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case HXRequestString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError *__autoreleasing *error) {
                return parameters;
            }];
            break;
            
        default:
            break;
    }
    
    // 3.为网络请求加请求头
    if (headerFile) {
        for (NSString *key in headerFile.allKeys) {
            [manager.requestSerializer setValue:headerFile[key] forHTTPHeaderField:key];
        }
    }
    // 4.网络请求返回值的数据类型
    switch (result) {
        case HXData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case HXJSON:
            break;
        case HXXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    
    // 5.设置网络请求返回值支持的类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain",@"application/x-javascript" ,nil]];
    
    // 6.发送网络请求
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject) {
            success(responseObject);
            
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            
            // 存储的沙盒路径
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            
            //             NSLog(@"--------%@",path_doc);
            
            // 归档
            [NSKeyedArchiver archiveRootObject:responseObject toFile:[path_doc stringByAppendingPathComponent:path]];
            
        }

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        if (error) {
            failure(error);
            
            // 缓存的文件夹
            NSString *path = [NSString stringWithFormat:@"%ld.plist", [url hash]];
            NSString *path_doc = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            id result = [NSKeyedUnarchiver unarchiveObjectWithFile:[path_doc stringByAppendingPathComponent:path]];
            success(result);
            
        }

    }];

}


@end
